package com.rdxer.sso.web.db;

import com.rdxer.db.migration.Config;
import com.rdxer.db.migration.MigrationManager;
import com.rdxer.db.migration.core.ActionMigration;
import com.rdxer.db.migration.core.FileMigration;
import com.rdxer.sso.web.model.Account;
import com.rdxer.sso.web.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Order(10)
public class MigrationRunner implements ApplicationRunner {

    @Resource
    MigrationManager migrationManager;
    @Autowired
    AccountService accountService;

    @Override
    public void run(ApplicationArguments args) {
        migrationManager.setConfig(Config.of(Config.DBType.mysql))
                .registerMigration(
                        // SQL文件迁移
                        FileMigration.of("v1", "sql/v1.sql")
                )
                .registerMigration(
                        // 代码迁移
                        ActionMigration.of("添加管理员", (migrationManager, status) -> {

                            Account admin = new Account();

                            admin.setName("admin");
                            admin.setEmail("admin@11.com");

                            accountService.save(admin);

                            return true;
                        })
                )
                .run();
    }
}
